package panel.mainPanel
{
	import assists.HtmlText;
	import assists.TextFieldUtils;
	import assists.log.Log;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class TextBox extends Sprite
	{
		private var _stage:Stage
		
		private var _width:Number = 0;
		private var _height:Number = 0;
		
		private var text:TextField;
		private var back:Sprite;
		
		private var thisObj:Object;
		
		private var onShowDetail:Function;
		
		private var clickText:TextField;
		
		private var downLoc:Number = 100000;
		private var upLoc:Number = 0;
		
		public function TextBox(stage:Stage,wid:Number,hei:Number)
		{
			_stage = stage;
			
			_width = wid;
			_height = hei;
			
			initThis();
		}
		
		private function initThis():void
		{
			back = new Sprite;
			back.graphics.beginFill(0x000000,0);
			back.graphics.drawRect(0,0,_width,_height);
			back.graphics.endFill();
			this.addChild(back);
			
			text = new TextField();
			text.multiline = true;
			text.wordWrap = true;
			
			text.width = _width;
			text.height = _height * 2 / 3;
			text.selectable = false;
			this.addChild(text);
			
			clickText = new TextField;
			clickText.multiline = false;
			clickText.wordWrap = false;
			clickText.width = _width;
			clickText.selectable = false;
			
			this.addChild(clickText);
			
			clickText.x = 0;
			clickText.y = _height * 2/3;
			TextFieldUtils.setHtmlText(clickText,HtmlText.formatSizeColor("<u><a href='event:click'>点击查看详情</a></u>",0xffff00,18),true,false);
			clickText.addEventListener(TextEvent.LINK,clickHandler);
			text.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			text.addEventListener(MouseEvent.MOUSE_UP,upHandler);
		}		
		
		protected function upHandler(event:MouseEvent):void
		{
			upLoc = event.localY;
			
			if(Math.abs(upLoc - downLoc) < _stage.stageHeight * .05){
				if(onShowDetail is Function){
					onShowDetail(thisObj);
				}
			}
		}
		
		protected function downHandler(event:MouseEvent):void
		{
			downLoc = event.localY;
		}
		
		protected function clickHandler(event:TextEvent):void
		{
			if(onShowDetail is Function){
				onShowDetail(thisObj);
			}
		}
		
		public function changeData(obj:Object):void
		{
			thisObj = obj;
			
			var str:String = obj.newsContent;
			if(str.length > 65){
				str = str.slice(0,65) + "···";
			}
			
			TextFieldUtils.setHtmlText(text,HtmlText.formatSizeColor(str,0xffff00,14),true);
		}
		
		public function set clickFunc(func:Function):void
		{
			onShowDetail = func;
		}
		
		public function clearRecord():void
		{
			downLoc = 100000;
		}
		
		public function clear():void
		{
			clickText.removeEventListener(TextEvent.LINK,clickHandler);
			text.removeEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			text.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
			
			back = null;
			clickText = null;
			
		}
	}
}